<template>
  <div>
    <el-button :icon="Plus" @click="dialogVisible = true" size="small">新增</el-button>
    <el-button :icon="Plus" size="small">导入</el-button>
    <el-button :icon="Upload" size="small">导出</el-button>
    <el-table :data="socEventConditionTypeList">
      <el-table-column prop="conditionTypeId" label="条件Id"></el-table-column>
      <el-table-column prop="conditionTypeKey" label="条件key"></el-table-column>
      <el-table-column prop="conditionTypeName" label="条件名称"></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="scope">
          <el-button size="small" @click="openUpdate(scope.row.eventId)" :icon="EditPen">编辑</el-button>
          <el-button size="small" type="danger" @click="openDel( scope.row.eventId)" :icon="Delete">删除</el-button>
          <el-divider direction="vertical"/>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="dialogVisible" :title="isAdd ? '新增':'修改'" width="500">
      <div>
        <el-form inline :model="conditionTypeCreate" ref="conditionTypeCreateFormRef" label-width="80">
          <el-form-item label="类型标识">
            <el-input v-model="conditionTypeCreate.conditionTypeKey" placeholder="请输入类型"/>
          </el-form-item>
          <el-form-item label="类型名称">
            <el-input v-model="conditionTypeCreate.conditionTypeName" placeholder="请输入类型名称"/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive, onMounted, watch} from "vue";
import {Download, Refresh, Delete, EditPen, Plus, Upload, View} from "@element-plus/icons-vue";
import type {FormInstance, FormRules} from "element-plus";
import {ElMessage, ElMessageBox} from "element-plus";
import type {SocEventConditionsTypes} from "@/api/social/event/type";
import {
  reqGetEventConditionTypeList,
  reqDelEventConditionType,
  reqGetEventConditionTypeById,
  reqAddEventConditionType,
  reqUpdateEventConditionType
} from "@/api/social/event";

let loading = ref<boolean>(true);
let socEventConditionTypeList = ref<SocEventConditionsTypes[]>([])
let dialogVisible = ref<boolean>(false)
let conditionTypeCreate = reactive({
  conditionTypeId: null,
  conditionTypeKey: '',
  conditionTypeName: '',
  createTime: null
})
let isAdd = ref<boolean>(true)
let conditionTypeCreateFormRef = ref<FormInstance>();

let getSocEventConditionTypeList = () => {
  reqGetEventConditionTypeList().then((res) => {
    socEventConditionTypeList.value = res.data;
    loading.value = false;
  });
};

let openUpdate = (conditionTypeId: string) => {
  isAdd.value = true;
  reqGetEventConditionTypeById(conditionTypeId).then((res) => {
    Object.assign(conditionTypeCreate, res.data)
  })
};
let openDel = (bardId: string) => {
  ElMessageBox.confirm("删除之后不能恢复，确认删除吗？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
      .then(() => {
        reqDelEventConditionType(bardId).then(() => {
          getSocEventConditionTypeList();
          ElMessage({
            message: "删除成功！",
            type: "success",
          });
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除！",
        });
      });
};
let submitForm = () => {
  if (isAdd.value) {
    reqAddEventConditionType(conditionTypeCreate).then(() => {
      ElMessage({
        message: "添加成功！",
        type: "success",
      });
      dialogVisible.value = false
    })
  } else {
    reqUpdateEventConditionType(conditionTypeCreate).then(() => {
      ElMessage({
        message: "修改成功！",
        type: "success",
      });
      dialogVisible.value = false
    })
  }
}

onMounted(() => {
  getSocEventConditionTypeList();
});
</script>
